package com.iims.flowable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.exception.CustomException;
import com.iims.common.utils.StringUtils;
import com.iims.flowable.common.constant.FlowableConstant;
import com.iims.flowable.flow.CommonUtil;
import com.iims.flowable.flow.domain.FlowTaskForm;
import com.iims.flowable.flow.vo.resp.HistoryFlowTaskFormVo;
import com.iims.flowable.mapper.FlowTaskFormMapper;
import com.iims.flowable.service.IFlowTaskFormService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/7/5 15:12
 */
@Service
public class FlowTaskFormServiceImpl extends ServiceImpl<FlowTaskFormMapper, FlowTaskForm> implements IFlowTaskFormService {


    @Override
    public String getRenderedStartTaskFormBack(String procInstId) {
        return getRenderedTaskFormBack(procInstId, FlowableConstant.PROCESS_INSTANCE_FORM_DATA);
    }

    @Override
    public String getRenderedTaskFormBack(String procInstId, String formKey) {
        LambdaQueryWrapper<FlowTaskForm> queryWrapper = new LambdaQueryWrapper<FlowTaskForm>()
                .select(FlowTaskForm::getFormDataBack)
                .eq(FlowTaskForm::getProcInstId, procInstId)
                .eq(FlowTaskForm::getFormKey, formKey);
        FlowTaskForm flowTaskForm = baseMapper.selectOne(queryWrapper);
        if (flowTaskForm == null)
            return "";
        else
            return flowTaskForm.formDataBackStr();
    }

    @Override
    public FlowTaskForm getRenderedTaskFormWithDataJson(String procInstId, String formKey) {
        LambdaQueryWrapper<FlowTaskForm> queryWrapper = new LambdaQueryWrapper<FlowTaskForm>()
                .select(FlowTaskForm::getFormDataJson, FlowTaskForm::getCreateBy)
                .eq(FlowTaskForm::getProcInstId, procInstId)
                .eq(FlowTaskForm::getFormKey, formKey);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<HistoryFlowTaskFormVo> getRenderedTaskForms(String procInstId) {
        return baseMapper.selectTaskFormDatasByProcInsId(procInstId);
    }

    @Override
    public void insertUpdateTaskForm(FlowTaskForm taskForm) {
        if (CommonUtil.isEmptyObject(taskForm.getFormDataBack())
                && CommonUtil.isEmptyObject(taskForm.getFormDataJson())) {
            throw new CustomException("formDataJson和 formDataBack 不能为空!");
        }

        LambdaQueryWrapper<FlowTaskForm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FlowTaskForm::getProcInstId, taskForm.getProcInstId())
                .eq(FlowTaskForm::getFormKey, taskForm.getFormKey());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            LambdaUpdateWrapper<FlowTaskForm> updateWrapper = new LambdaUpdateWrapper<FlowTaskForm>();
            updateWrapper
                    .eq(FlowTaskForm::getProcInstId, taskForm.getProcInstId())
                    .eq(FlowTaskForm::getFormKey, taskForm.getFormKey())
                    .set(FlowTaskForm::getProcDefId, taskForm.getProcDefId())
                    .set(FlowTaskForm::getFormKey, taskForm.getFormKey())
                    .set(FlowTaskForm::getFormDataBack, taskForm.getFormDataBack())
                    .set(FlowTaskForm::getFormDataJson, taskForm.getFormDataJson())
                    .set(FlowTaskForm::getTaskName, taskForm.getTaskName())
                    .set(FlowTaskForm::getCreateBy, taskForm.getCreateBy());
            baseMapper.update(null, updateWrapper);
        } else {
            baseMapper.insert(taskForm);
        }
    }

    @Deprecated
    @Override
    public void updateTaskFormProInstanceId(FlowTaskForm taskForm) {
        LambdaUpdateWrapper<FlowTaskForm> updateWrapper = new LambdaUpdateWrapper<FlowTaskForm>();
        updateWrapper
                .eq(FlowTaskForm::getTaskId, taskForm.getTaskId())
                .set(StringUtils.isNotEmpty(taskForm.getProcInstId()), FlowTaskForm::getProcInstId, taskForm.getProcInstId());
        baseMapper.update(null, updateWrapper);
    }
}
